package ntd.util.frame.javacv.ai_fuming;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Filter;
import cn.hutool.cron.CronUtil;
import cn.hutool.extra.ftp.Ftp;
import cn.hutool.setting.dialect.Props;
import ntd.util.frame.javacv.AiModelBean;
import org.apache.commons.net.ftp.FTPFile;

import java.util.List;

public class AiClearFileData {

    /*清除过期的图片、视频的原始文件
        1、每天凌晨两点执行任务
        2、清除两天前的所有数据
     */
    public static void main(String[] args) {
        //每天凌晨两点
        CronUtil.schedule("0 0 2 * * ?", (Runnable) () -> {
            //获取配置文件
            Props prop = new Props("ai-config.properties");

            int timeoutDay = 0;//删除一天前的数据

            String imagePath = prop.getStr("ai-server-meta-data-base-path")+"image/";
            String videoPath = prop.getStr("ai-server-meta-data-base-path")+"video/";
            AiModelBean aiModelBean = new AiModelBean();
            for (AiModelBean modelBean: aiModelBean.getModelList()){
                Ftp ftp = new Ftp(modelBean.getFtpIp(), prop.getInt("ai-ftp-port"), prop.getStr("ai-ftp-username"), prop.getStr("ai-ftp-password"));

                //删除图片
                List<FTPFile> imageCameraDirs = ftp.lsFiles(imagePath,FTPFile::isDirectory);
                if (imageCameraDirs != null && imageCameraDirs.size() > 0){
                    for (FTPFile imageCameraDir:imageCameraDirs){

                        if (!ftp.exist(imagePath+imageCameraDir.getName())) continue;

                        List<FTPFile> imageDateDir = ftp.lsFiles(imagePath+imageCameraDir.getName(),FTPFile::isDirectory);

                        for (FTPFile imageDate:imageDateDir){
//                            StaticLog.info("=="+DateUtil.between(DateUtil.date(),DateUtil.parse(imageDate.getName()), DateUnit.DAY));
                            if (DateUtil.between(DateUtil.date(),DateUtil.parse(imageDate.getName()), DateUnit.DAY) > timeoutDay){
                                //删除前一天的数据
                                ftp.delDir(imagePath+imageCameraDir.getName()+"/"+imageDate.getName());
                            }
                        }


                    }
                }


                //删除视频
                Filter<FTPFile> ftpFileFilter = ftpFile -> ftpFile.getName().contains("-video");

                List<FTPFile> videoCameraFiles = ftp.lsFiles(videoPath,ftpFileFilter);
                if (videoCameraFiles != null && videoCameraFiles.size() > 0){
                    for (FTPFile video:videoCameraFiles){
//                        StaticLog.info(video.getTimestamp().getTime()+"=="+DateUtil.between(DateUtil.date(),video.getTimestamp().getTime(), DateUnit.DAY));
                        if (DateUtil.between(DateUtil.date(),video.getTimestamp().getTime(), DateUnit.DAY) > timeoutDay){
//                            StaticLog.info("=="+videoPath+video.getName());
                            ftp.delFile(videoPath+video.getName());
                        }
                    }
                }

            }
        });

      CronUtil.start();


    }
}
